package cn.bellychang.ood.payroll.affiliation;

import cn.bellychang.ood.payroll.domain.Affiliation;
import cn.bellychang.ood.payroll.domain.Paycheck;
import cn.bellychang.ood.payroll.domain.ServiceCharge;
import lombok.Builder;
import lombok.Data;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;

import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ChangLiang
 * @date 2020/5/19
 */
@Data
@Builder
public class UnionAffiliation implements Affiliation {


    private Map<Date, ServiceCharge> deductionMap;

    @Override
    public Money calculateDeduction(Paycheck paycheck) {
        Money deduction = Money.of(CurrencyUnit.of("CNY"), 0);
        try {
            deduction = Money.total(deductionMap.values().stream()
                    .filter(sc -> sc.getDate().before(paycheck.getPayPeriodEnd()) && sc.getDate().after(paycheck.getPayPeriodStart()))
                    .map(sc -> sc.getAmount()).collect(Collectors.toList()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deduction;
    }
}
